// 没有加载标识
import axios from 'axios';
import Vue from 'vue';

import store from '@/Store/index'



// let env = 'prod';  //dev开发, test测试, prod生产
// let baseURL;
// if (env === 'dev') {
//   baseURL = 'http://localhost:3003';
// } else if (env === 'test') {
//   baseURL = 'http://test.huruqing.cn:3003';
// } else {
//   baseURL = 'http://81.71.65.4:3003';
//   // baseURL = 'http://huruqing.cn:3003';
// }
let baseURL = process.env.VUE_APP_URL;



const service = axios.create({
  baseURL,
  timeout: 30000
})

// 配置请求拦截
service.interceptors.request.use(
  (config) => {


    // console.log(config);
    // 添加token
    config.headers["user-token"] = store.state.token;
    return config;
  },
  (error) => {
    console.log('request.js请求拦截失败');
    return Promise.reject(error);
  }
);

// 配置响应拦截
service.interceptors.response.use(
  (res) => {


    // console.log('request.js成功获取数据');
    if (res.data.code == 666) {
      return res.data;
    } else {
      console.log('request.js获取数据错误，code不为666', res);

      // alert('失败')
    }
  },
  (error) => {

    console.log('request.js没有获取到返回的数据');
    let { message } = error;
    if (message == "Network Error") {
      message = "后端接口连接异常";
    }
    else if (message.includes("timeout")) {
      message = "系统接口请求超时";
    }
    else if (message.includes("Request failed with status code")) {
      message = "系统接口" + message.substr(message.length - 3) + "异常";
    }

    return Promise.reject(message);
  }

);

export function get(url, params) {
  return service.get(url, { params })
}
export function post(url, data) {
  return service.post(url, data)
}
// export default service;